package cc.vv.btparty.view

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import cc.vv.btpartyjob.partymanager.R
import cc.vv.lkbasecomponent.util.LKScreenUtil

/**
 * 评星晋级自定义view
 * Created by Chen.qi on 2018/9/14.
 */
class RatingStarView : LinearLayout {

    /**
     * 最大星星数量
     */
    var maxStart: Int = 5

    private var llStar: LinearLayout? = null


    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        val inflateView = View.inflate(context, R.layout.layout_rating_star, null)
        llStar = inflateView.findViewById(R.id.ll_ratingStar_top)
        this.addView(inflateView)
    }


    /**
     * 设置总的星星数量,默认是5颗星星
     */
    fun setTotalStarts(maxStart: Int) {
        this.maxStart = maxStart
    }


    /**
     * 填充数据逻辑
     */
    fun initData(showStart: Int) {
        llStar?.removeAllViews()
        val wight = LKScreenUtil.dp2px(20f)
        val height = LKScreenUtil.dp2px(20f)
        for (i in 0 until maxStart) {
            if (showStart <= maxStart) {
                val imageView = ImageView(context)
                imageView.layoutParams = LinearLayout.LayoutParams(wight, height)
                imageView.setPadding(5, 0, 0, 0)
                if (i < showStart) {
                    imageView.setImageResource(R.mipmap.icon_homepage_star_zc)
                } else {
                    imageView.setImageResource(R.mipmap.icon_homepage_star_gray)
                }
                llStar?.addView(imageView)
            }
        }
    }
}